// 工厂方法, 用于创建 一个 store module
import moduleCreator from '../moduleCreator'

import api from '@/api'

// moduleName 指向了
const modelName = 'report.accounting_pdf_reports.report_partnerledger'
const wiardModelName = 'account.report.partner.ledger'

const Model = api.env(modelName)
const wiardModel = api.env(wiardModelName)

const wizardFields = {
  target_move: null,
  result_selection: null,
  reconciled: null,
  date_from: null,
  date_to: null,
  journal_ids: { name: null, display_name: null },
  company_id: null
}

const baseModel = {
  async report_wizard_create(payload) {
    const { values } = payload

    const res = await wiardModel.create(values, { fields: wizardFields })
    console.log('report_wizard_create,', res)
    return res
  },

  async report_print(payload) {
    const { values } = payload
    const res = await Model.report_print({
      model: wiardModelName,
      values,
      fields: wizardFields
    })
    console.log('report_print,', res)
    return res
  }
}

const fields = {}

const config = {
  model: modelName,
  fieldsForSearch: fields,
  fieldsForBrowse: fields
}

const state = {
  //
  // options: []
}
const mutations = {
  // SET_Options: (state, payload) => {
  //   state.options = payload
  // }
}
const actions = {
  async report_print({ commit, state, dispatch }, payload) {
    const { partner_id, result_selection } = payload
    const values = { result_selection }

    const res = await baseModel.report_wizard_create({ values })

    // console.log('report_wizard_create', partner_id, res)

    const res2 = await baseModel.report_print({
      values: { id: res.id }
    })

    const rpts = res2.report_data
    const paertner_rpts = rpts.filter(item => item.partner_id === partner_id)
    const paertner_rpt = paertner_rpts.length ? paertner_rpts[0] : {}

    commit('SET_DataList', paertner_rpts)
    commit('SET_DataDict', paertner_rpt)

    return new Promise(resolve => {
      resolve(paertner_rpt)
    })
  }
}

export default moduleCreator(config, {
  state,
  mutations,
  actions
})

// // 这是最简单的写法
// export default moduleCreator(modelName)

// 如果有扩展, 则按照下面的写法
// const state = {
//   //
// }
// const mutations = {
//   //
// }
// const actions = {
//   //
// }

// export default moduleCreator(modelName, {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// })

// 如果 不继承, 用下面的写法
// const state = {
//   //
// }
// const mutations = {
//   //
// }
// const actions = {
//   //
// }

// export default {
//   namespaced: true,
//   state,
//   mutations,
//   actions
// }
